﻿using System;
using System.Drawing;

namespace AB
{
    public class Trial
    {
        public const int MIN_PRE_STIM = 7;
        public const int MAX_PRE_STIM = 15;
        public const int NUM_POST_STIM = 8;

        public const int SOA_TIME = 100;
        public const int ISI_TIME = 80;

        public const String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        String remainingStim = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        int currentPosition;
        int targetPosition;
        public int probePosition;
        int maxStim;

        String probe;
        public String target;
        public String targetGuess;
        public Boolean probePresent;
        Random random;

        public Boolean correctTarget;
        public Boolean correctProbe;

        public DateTime startTime;
        public DateTime endTime;

        public Trial(Boolean probePresent, int position)
        {
            random = new Random();

            remainingStim = ALPHABET;
            probe = "X";
            //probePresent = (random.NextDouble() > 0.5);
            this.probePresent = probePresent;

            remainingStim = remainingStim.Remove(remainingStim.IndexOf(probe), 1);

            probePosition = position;
        }

        public void startTrial()
        {
            random = new Random();

            currentPosition = 0;

            targetPosition = random.Next(MIN_PRE_STIM, MAX_PRE_STIM + 1);

            //probePosition = (random.Next(0, NUM_POST_STIM) + 1);

            maxStim = targetPosition + 8;

        }

        public String getNextStim()
        {
            random = new Random();
            if ((currentPosition == probePosition + targetPosition) && probePresent)
            {
                return probe;
            }
            else
            {
                int next = random.Next(0, remainingStim.Length);
                String stimulus;
                if (currentPosition == targetPosition)
                {
                    target = remainingStim.Substring(next, 1);
                    stimulus = target;
                }
                else
                {
                    stimulus = remainingStim.Substring(next, 1);
                }
                remainingStim = remainingStim.Remove(remainingStim.IndexOf(stimulus),1);
                return stimulus;
            }
            
        }

        public Boolean anotherStimulus()
        {
            currentPosition++;
            return currentPosition <= maxStim;
        }

        public void validateInput(String targetInput, Boolean probePresentInput)
        {
            // If we're validating input, that means trial is over, so stop time
            endTime = DateTime.Now;
            this.targetGuess = targetInput;
            correctTarget = (target == targetInput);
            correctProbe = (probePresent == probePresentInput);

        }

        public Color getColor()
        {
            if (currentPosition == targetPosition) 
            {
                return Color.White;
            } 
            else
            {
                return Color.Black;
            }
        }

        public int getSOATime()
        {
            return SOA_TIME;
        }

        public int getISITime()
        {
            return ISI_TIME;
        }

        public String getTarget()
        {
            return target;
        }

        public Boolean getProbePresent()
        {
            return probePresent;
        }

        public TimeSpan getTrialTime()
        {
            return endTime - startTime;
        }


    }
}
